package com.example.callloggenerator;

import android.content.Context;
import android.os.Build;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.util.Log;

import java.util.List;
import java.util.Random;

//实用工具类

public class CallLogUtils {

    private static final String[] PHONE_PREFIXES = {"13", "14", "15", "17", "18"};
    private static final String[] PHONE_400_PREFIXES = {"184006"};
    private static final int MAX_PHONE_NUMBER_LENGTH = 11;

    // 随机生成电话号码的方法
    public static String generateRandomPhoneNumber() {
        Random random = new Random();
        StringBuilder phoneNumber = new StringBuilder();
        phoneNumber.append(PHONE_PREFIXES[random.nextInt(PHONE_PREFIXES.length)]);
        while (phoneNumber.length() < MAX_PHONE_NUMBER_LENGTH) {
            phoneNumber.append(random.nextInt(10));
        }
        return phoneNumber.toString();
    }

    // 随机生成400电话号码的方法
    public static String generateRandom400PhoneNumber() {
        Random random = new Random();
        StringBuilder phoneNumber = new StringBuilder();
        phoneNumber.append(PHONE_400_PREFIXES[random.nextInt(PHONE_400_PREFIXES.length)]);
        while (phoneNumber.length() < MAX_PHONE_NUMBER_LENGTH) {
            phoneNumber.append(random.nextInt(10));
        }
        return phoneNumber.toString();
    }

    // 获取默认SIM卡的订阅ID（仅适用于API 24及以上）
    public static int getDefaultSubscriptionId(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            SubscriptionManager subscriptionManager =
                    (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
            if (subscriptionManager == null) return -1;

            List<SubscriptionInfo> activeSubscriptionInfoList = subscriptionManager.getActiveSubscriptionInfoList();
            if (activeSubscriptionInfoList == null || activeSubscriptionInfoList.isEmpty()) {
                return -1;
            }

            int defaultDataSubId = subscriptionManager.getDefaultDataSubscriptionId();
            int defaultVoiceSubId = subscriptionManager.getDefaultVoiceSubscriptionId();

            return defaultVoiceSubId != SubscriptionManager.INVALID_SUBSCRIPTION_ID ? defaultVoiceSubId : defaultDataSubId;
        }
        return -1;
    }
}